package de.hsma.srn.model.account;

import de.hsma.srn.controller.MainController;
import de.hsma.srn.model.user.UserListModel;
import de.hsma.srn.model.user.UserModel;

public class DomainModel {
	private UserListModel mUserList;
	private AccountModel mCurrentAccount;
	private MainController mMainController;
	
	
	public boolean isLoggedIn() {
		if(this.mCurrentAccount!=null) {
			return true;
		}
		return false;
	}
	public UserListModel getUserList() {
		return this.mUserList;
	}
	public AccountModel getAccountModel() {
		return this.mCurrentAccount;
	}
	public void setController(MainController mainController) {
		this.mMainController = mainController;
	}
	
	
	public DomainModel() {
		this.mUserList = new UserListModel();
	}
	
	
	public boolean login(String username, String password) {
		UserModel user = this.mUserList.get(username);
		if(user!=null) {
			if(user.checkPassword(password)) {
				this.mCurrentAccount = new AccountModel(user, password);
				return true;
			}
		}
		return false;
	}
	
	public boolean logout() {
		if(this.mCurrentAccount!=null) {
			this.mCurrentAccount = null;
			if(this.mMainController!=null) {
				this.mMainController.update();
			}
			return true;
		}
		return false;
	}
	
	public boolean register(String username, String password) {
		UserModel user = new UserModel(username, password);
		if(this.mUserList.add(user)) {
			return true;
		} else {
			return false;
		}
	}
}
